package com.xlh.support.experimentshare.controller;

import com.xlh.common.Result;
import com.xlh.security.UserInfoHolder;
import com.xlh.support.experimentshare.domain.ShareUser;
import com.xlh.support.experimentshare.po.ShareExperimentPO;
import com.xlh.support.experimentshare.service.ExperimentShareService;
import com.xlh.support.experimentshare.vo.ExperimentUserVO;
import com.xlh.support.experimentshare.vo.ShareExperimentVO;
import com.xlh.support.experimentshare.vo.ShareUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@Api("实验分享")
@RestController
@RequestMapping("/experiment/share")
public class ExperimentShareController {

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private ExperimentShareService experimentShareService;

    @ApiOperation("列出该课程的用户")
    @GetMapping("/{chapterId}/users")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result<List<ExperimentUserVO>> queryExperimentUsers(@PathVariable("chapterId") Long chapterId) {
        List<ExperimentUserVO> experimentUserVOS = experimentShareService.listExperimentUsers(userInfoHolder.getUserId(),
                userInfoHolder.isTeacher(), chapterId);
        return Result.success(experimentUserVOS);
    }

    @ApiOperation("分享桌面")
    @PostMapping("/{chapterId}")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result shareExperiment(@PathVariable("chapterId") Long chapterId,
                                  @RequestBody @Valid ShareExperimentPO shareExperimentPO) {
        ShareUser shareUser = ShareUser.builder()
                .userId(userInfoHolder.getUserId())
                .chapterId(chapterId)
                .username(userInfoHolder.getUsername())
                .build();
        experimentShareService.shareExperiment(shareUser, shareExperimentPO.getUsers(), shareExperimentPO.getControllable());
        return Result.success();
    }

    @ApiOperation("关闭分享")
    @GetMapping("/{chapterId}/close")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result closeShareExperiment(@PathVariable("chapterId") Long chapterId) {
        ShareUser shareUser = ShareUser.builder()
                .userId(userInfoHolder.getUserId())
                .chapterId(chapterId)
                .username(userInfoHolder.getUsername())
                .build();
        experimentShareService.closeShareExperiment(shareUser);
        return Result.success();
    }

    @ApiOperation("查询收到的分享桌面列表")
    @GetMapping
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result<List<ShareUserVO>> queryShareExperiment() {
        List<ShareUserVO> shareUserVOS = experimentShareService.queryShareExperiment(userInfoHolder.getUserId());
        return Result.success(shareUserVOS);
    }

    @ApiOperation("进入分享桌面")
    @GetMapping("/{userId}/{chapterId}")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result<ShareExperimentVO> enterShareExperiment(@PathVariable("userId") Long userId,
                                                          @PathVariable("chapterId") Long chapterId) {
        ShareUser shareUser = ShareUser.builder()
                .userId(userId)
                .chapterId(chapterId)
                .build();
        ShareExperimentVO shareExperimentVO = experimentShareService.enterShareExperiment(userInfoHolder.getUserId(), shareUser);
        return Result.success(shareExperimentVO);
    }

    @ApiOperation("删除分享桌面")
    @DeleteMapping("/{userId}/{chapterId}")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result deleteShareExperiment(@PathVariable("userId") Long shareUserId,
                                        @PathVariable("chapterId") Long chapterId) {
        ShareUser shareUser = ShareUser.builder()
                .userId(shareUserId)
                .chapterId(chapterId)
                .build();
        experimentShareService.deleteShareExperiment(userInfoHolder.getUserId(), shareUser);
        return Result.success();
    }

    @ApiOperation("删除所有分享桌面")
    @DeleteMapping
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result deleteAllShareExperiment() {
        experimentShareService.deleteAllShareExperiment(userInfoHolder.getUserId());
        return Result.success();
    }

    @ApiOperation("分享者切换分享桌面窗口")
    @GetMapping("/switch/{chapterId}")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result switchExperiment(@PathVariable("chapterId") Long chapterId,
                                   @RequestParam Integer number) {
        ShareUser shareUser = ShareUser.builder()
                .userId(userInfoHolder.getUserId())
                .chapterId(chapterId)
                .username(userInfoHolder.getUsername())
                .build();
        experimentShareService.switchExperiment(shareUser, number);
        return Result.success();
    }
}
